# coding=utf-8

from flask import current_app, jsonify, request
from app.close_ssh import close_ssh
from app.conn_ssh import conn_ssh
from app.get_monitor_datas import get_TableSpace_datas, get_CpuAndMem_datas, get_Real_CpuAndMem_datas
from manage import app
from . import main
from config import config


@main.before_request
def bef_request():
    pass


@main.after_request
def aft_request(param):
    pass
    return param


# 数据库表空间相关数据获取
@main.route('/show_tablespace_chart', methods=['POST', 'GET'])
def show_tablespace_chart():
    current_app.logger.info('----------This is show_tablespace_chart----------')
    try:
        praSql = config['default'].GET_TABLESPACE_SQL
        datas = get_TableSpace_datas(praSql)
    except Exception as e:
        current_app.logger.error(e.message)
        return e.message
    else:
        return jsonify(datas)


# 主机CPU及内存实时使用情况数据获取
@main.route('/show_real_cpuAndmem_chart', methods=['POST', 'GET'])
def show_real_cpuAndmem_chart():
    hostId = request.form.get("host")
    app.logger.info('----------This is show_real_cpuAndmem_chart----------')
    try:
        monitorHosts = config['default'].MONITOR_HOSTS
        monitorCommand = config['default'].MONITOR_COMMAND
        monitor_datas = get_Real_CpuAndMem_datas(hostId, monitorHosts, monitorCommand)
    except Exception as e:
        current_app.logger.error(e.message)
        return e.message
    else:
        return jsonify(monitor_datas)


# 主机CPU及内存趋势使用情况数据获取
@main.route('/show_cpuAndmem_chart', methods=['POST', 'GET'])
def show_cpuAndmem_chart():
    current_app.logger.info('----------This is show_cpuAndmem_chart----------')
    hostId = request.form.get("host")
    current_app.logger.info('----------This is hostId---------- %s', hostId)
    try:
        monitorHosts = config['default'].MONITOR_HOSTS
        monitorCommand = config['default'].MONITOR_COMMAND
        monitor_datas = get_CpuAndMem_datas(hostId, monitorHosts, monitorCommand)
    except Exception as e:
        current_app.logger.error(e.message)
        return e.message
    else:
        return jsonify(monitor_datas)


# 主机磁盘使用情况数据获取
@main.route('/show_shareHD_chart', methods=['POST', 'GET'])
def show_shareHD_chart():
    current_app.logger.info('----------This is show_shareHD_chart----------')
    ssh = conn_ssh(config['default'].CONNECT_SSH)
    command = config['default'].GET_SHARE_HD
    stdin, stdout, sterr = ssh.exec_command(command[0])
    stdoutStr = str(stdout.read())
    result_data1 = stdoutStr.split('\n')
    result_new_data1 = []
    for i in range(len(result_data1)):
        if result_data1[i] == '':
            break
        else:
            result_new_data1.append(result_data1[i])

    stdin, stdout, sterr = ssh.exec_command(command[1])
    stdoutStr = str(stdout.read())
    result_data2 = stdoutStr.split('\n')
    result_new_data2 = []
    for i in range(len(result_data2)):
        if result_data2[i] == '':
            break
        else:
            # current_app.logger.info("int(result_data2[i]:" + result_data2[i])
            result_new_data2.append(int(result_data2[i]) / 1024 / 1024)

    stdin, stdout, sterr = ssh.exec_command(command[2])
    stdoutStr = str(stdout.read())
    result_data3 = stdoutStr.split('%\n')
    result_new_data3 = []
    for i in range(len(result_data3)):
        if result_data3[i] == '':
            break
        else:
            result_new_data3.append(result_data3[i])

    datas = (result_new_data1, result_new_data2, result_new_data3)
    close_ssh(ssh)

    return jsonify(datas)

